<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

class ExpectedProductsComponent
{
	function ExpectedProductsComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);

		$languageId = (int)$params['languageId'];

		$paginate = (boolean)$params['paginate'];
		$max_rows = (int)$params['max_rows'];
		$offset = (int)$params['offset'];

		$sqlQuery = "select p.products_id, pd.products_name, DATE_FORMAT(p.products_date_available, '%Y-%m-%d %H:%i:%s') as date_available, ptc.categories_id " .
		'from ' . TABLE_PRODUCTS_DESCRIPTION . ' pd, ' . TABLE_PRODUCTS . ' p, ' . TABLE_PRODUCTS_TO_CATEGORIES . ' ptc ' .
		'where p.products_id=pd.products_id and p.products_id=ptc.products_id ' .
		'and p.products_date_available != \'\' ' . 
		'and pd.language_id = '.$languageId . ' ' .
		'order by p.products_date_available DESC';
		if ($paginate) {
			$sqlQuery .= ' limit ' . $offset . ', ' . $max_rows;
		}
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'categories_id'}= (int)$row['categories_id'];
			$value{$handle}{$i}{'id'}= (int)$row['products_id'];
			$value{$handle}{$i}{'name'}= $row['products_name'];
			$value{$handle}{$i}{'date_available'}= $row['date_available'];
			$i++;
		}

		if ($paginate) {

			$sqlQuery = 'select count(pd.products_id) as cnt ' .
			'from ' . TABLE_PRODUCTS_DESCRIPTION . ' pd, ' . TABLE_PRODUCTS . ' p ' .
			'where p.products_id=pd.products_id  and p.products_date_available != \'\' ' . 
			'and pd.language_id = '.$languageId;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$cnt = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$cnt = (int)$row['cnt'];
			}

			$value{$handle . 'MetaData'}{'max_rows'}= (int)$max_rows;
			$value{$handle . 'MetaData'}{'offset'}= $offset;
			$value{$handle . 'MetaData'}{'page_links_max'} = (int)MAX_DISPLAY_PAGE_LINKS;
			$value{$handle . 'MetaData'}{'rows'}= $i;
			$value{$handle . 'MetaData'}{'total_rows'}= $cnt;
		}

		return $value;
	}

	function edit(&$params) 
	{

		$languageId = (int)$params['languageId'];
		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 1000) break;

			$productId = (int)$params['productId-'.$i];
			if ($productId < 1) break;

			$availableDate = (int)$params['productAvailableDate-'.$i];

			if ($availableDate > 0) {
				$availableDate_sec = $availableDate/1000.00;
				$sql = 'update ' . TABLE_PRODUCTS . ' set products_date_available = \'' . date('Y-m-d',$availableDate_sec) . '\' where products_id=' . $productId;
				mysql_query($sql) or die(mysql_error());
			}
			else {
				$sql = 'update ' . TABLE_PRODUCTS . ' set products_date_available = null where products_id=' . $productId;
				mysql_query($sql) or die(mysql_error());
			}
			remove_cache($languageId . '_' . $productId . '_true', 'ProductComponent');
			remove_cache($languageId . '_' . $productId . '_false', 'ProductComponent');
		}


		return $this->query($params);
	}
}
?>
